<?php
session_start();

require_once '../class/user.class.php';
require_once '../class/chatroom.class.php';

//if(isset($_SESSION['user']))

$user = unserialize($_SESSION['user']);

if ($user == null)
    header("Location: ../index.php?message='you must login first!'");

//$user = new User(1, 'hippo', 'Do Ngoc Ha', 'hippo@gmail.com');
$chatroom = new Chatroom(1, 'Victoria School', 'Teaching the children how to control the robot', 1);

$username = $user->userName;

echo "<script>";
echo 'var name ="' . $username . '";';
echo "</script>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <?php
            $user_id = $user->userID;
            echo "<input type='hidden' id='user_id' value='$user_id'/>";
        ?>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            var refreshInterval;
            function runForever()
            {
                refreshInterval = setInterval(function(){
                    $.ajax("http://ramp.qv.vic.edu.au/nxt/run.php?json={\"nxt\":1,\"type\":0,\"command\":1}");}, 1000);
            }
            function run()
            {
                    $.ajax("http://ramp.qv.vic.edu.au/nxt/run.php?json={\"nxt\":1,\"type\":0,\"command\":1}");
            }
            function stop()
            {
                clearInterval(refreshInterval);
            }
        </script>
        <script>

            function sendMessage()
            {
                var chatroom_id = 1;
                var user_id = document.getElementById("user_id").value;
                var message_text = document.getElementById("sendie").value;
                
                $.ajax("sendMessage.php?chatroom_id="+chatroom_id+"&user_id="+user_id+"&message_text="+message_text+"");
                document.getElementById("sendie").value = "";
                $("#sendie").focus();
            }

            function getMessage()
            {
                $.get("getMessage.php", function(data, status)
                {
                    document.getElementById("chat-area").innerHTML = data;
                });
            }

            setInterval(function(){getMessage()}, 200);

        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Cerberus Chat</title>

        <link href="style.css" rel="stylesheet" type="text/css" />
        <link href='http://fonts.googleapis.com/css?family=Molengo' rel='stylesheet' type='text/css'/>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    </head>

    <body>
        <div id="header"></div>
        <div id="page-wrap">
            <div id="left">
                <div id="header">
                    USER LIST
                </div>
                <div class="controller active">h16pham</div>
                <div class="controller">n22le</div>
                <div class="controller">Obama</div>
                <div class="controller">Zues</div>
                <div class="controller">Titan</div>
                <div class="controller">Hello</div>
                <div class="controller">Nokia</div>
                <div class="controller">Samsung</div>
            </div>
            <div id="mid">
                <div id="lab">
                    <div id="title">
                        <?php
                        echo $chatroom->getChatroom_name();
                        ?>
                    </div>
                    <div id="teacher">
                        <?php
                        echo $chatroom->getChatroom_description();
                        ?>
                    </div>
                </div>
                <object class="video"> <param name="movie" value="http://fpdownload.adobe.com/strobe/FlashMediaPlayback_101.swf"></param><param name="flashvars" value="src=rtmp%3A%2F%2Fclusterdb.qv.vic.edu.au%2Flive%2F1&controlBarMode=none&poster=http%3A%2F%2Fosmf.org%2Fimages%2Fposter_cathy_fmp.jpg&autoPlay=true&streamType=live&bufferingOverlay=false&optimizeBuffering=false&initialBufferTime=.1&expandedBufferTime=.5"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://fpdownload.adobe.com/strobe/FlashMediaPlayback_101.swf" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="240" flashvars="src=rtmp%3A%2F%2Fclusterdb.qv.vic.edu.au%2Flive%2F1&controlBarMode=none&poster=http%3A%2F%2Fosmf.org%2Fimages%2Fposter_cathy_fmp.jpg&autoPlay=true&streamType=live&bufferingOverlay=false&optimizeBuffering=false&initialBufferTime=.1&expandedBufferTime=.5"></embed></object>
                <INPUT TYPE="BUTTON" VALUE="Task 1: Make the Robot run once" onclick="run()">
                <INPUT TYPE="BUTTON" VALUE="Task 2: Make the Robot run forever" onclick="runForever()">
                <INPUT TYPE="BUTTON" VALUE="Task 3: Make the Robot stop" onclick="stop()">
            </div>
            <div id="right">
                <div id="left-conner">    
                    <div id="user">
                        <div id="username">
                            <?php
                            echo $user->userLogin;
                            ?>
                        </div>
                        <div id="button" >
                            <a href="../logout.php">Log out</a>
                            <a href="../index.php">To the Homepage</a>
                        </div>
                    </div>
                </div>   
                <div id="chat">
                    <p class="demo-chat" id="chat-area">
                    </p>
                </div>
                <div id="space">
                </div>
                <div id="chat-screen">
                    <textarea class="chat" id="sendie" maxlength = '100' autofocus></textarea>
                    <button onclick="sendMessage()">Send</button>
                </div>
            </div>
            </html>